<template>
  <div class="LogoView">
    <div class="box">
      <h1>后台管理系统 <span>v4.0</span></h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="" prop="userName">
          <el-input v-model="ruleForm.userName" prefix-icon="el-icon-user-solid" placeholder="登录账号"></el-input>
        </el-form-item>
        <el-form-item label="" prop="pwd">
          <el-input v-model="ruleForm.pwd" prefix-icon="el-icon-lock" placeholder="登录密码"></el-input>
        </el-form-item>
        <el-form-item label="" prop="imgcode">
          <el-input v-model="ruleForm.imgcode" prefix-icon="el-icon-picture" placeholder="验证吗">
          </el-input>
          <img width="80px" height="36px" :src="imgtr" alt="" @click="getTmage" />
        </el-form-item>
        <el-form-item label="" prop="region">
          <el-select v-model="ruleForm.region" placeholder="简体中文">
            <el-option label="简体中文" value="1"></el-option>
            <el-option label="繁体中文" value="2"></el-option>
            <el-option label="English" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="resource">
          <el-radio-group v-model="ruleForm.resource">
            <el-checkbox label="记住登录状态"></el-checkbox>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script >
import { Logovalidate } from "@/api/index.js";
export default {
  data() {
    return {
      ruleForm: {

        userName: 'wode99',
        pwd: 'wode99',
        imgcode: '',
        language: "简体中文",
        k: '',
        pdomain: 'zhangzhengyan',
      },
      imgtr:'',
      rules: {
        // userName: [
        //   { required: true, message: '请输入账号', trigger: 'blur' },
        //   { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        // ],
        // pwd: [
        //   { required: true, message: '请输入密码', trigger: 'blur' },
        //   { min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        // ],
        // imgcode: [
        //   { required: true, message: '请输入验证吗', trigger: 'blur' },
        //   { min: 2, max: 8, message: '请重新输入', trigger: 'blur' }
        // ],
      },
      
    }

  },



  methods: {
    // 登录 图形验证
  async getTmage() {
      this.ruleForm.k = Math.random()
      this.imgtr = "https://user.api.it120.cc/code?k=" + this.ruleForm.k
    },

   // 登录请求
    // 在api里创建每个页面单独的axios请求,然后引入到vue页面里调用
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let a = await Logovalidate(this.ruleForm)
          if (a.code == 0) {
            sessionStorage.setItem("token", a.data.token)
            this.$message({
              message: '登录成功',
              type: 'success',
              duration: 500
            });
            setTimeout(() => {
              this.$router.push("/AboutView")
            }, 1000)

          } else {
            this.$message.error('输入错误');
            this.getTmage()
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
  mounted() {
    this.getTmage();
  },
}
</script>
<style lang="scss" scoped>
.LogoView {
  width: 100vw;
  height: 100vh;
  background-color: #2d3a4b;

  .box {
    width: 520px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);


    h1 {
      text-align: center;

      span {
        font-size: 16px;
      }
    }

    .el-select {
      width: 100%;
    }

    &::v-deep .el-input__inner {
      width: 100%;
      height: 52px;
      background-color: #2d3a4b;
      border: 0.5px solid #cccc;
      color: #ffffff;
    }

    select {
      width: 100%;
      height: 52px;
      background-color: #2d3a4b;
      border: 0.5px solid #cccc;
      color: #ffffff;
      outline: none;
    }

    button {
      width: 100%;
      height: 52px;
      border-radius: 5px;
    }

    &::v-deep .el-form-item__content {
      position: relative;

      img {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
}
</style>